package com.example.testspringwebflux;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StringReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.CharBuffer;

/**
 * ServerMain
 *
 * @author xule
 * @since 2024/07/08
 */
public class ServerMain {

    public static void main(String[] args) throws Exception {
        ServerSocket socket = new ServerSocket(8080);
        new Thread(()-> {
            while (true) {
                try {
                    Socket accept = socket.accept();
                    PrintStream printStream;
                    InputStream inputStream = accept.getInputStream();
                    InputStreamReader reader = new InputStreamReader(inputStream);
                    CharBuffer wrap = CharBuffer.wrap(new char[1024]);
                    if ((reader.read(wrap)) != -1) {

                        System.out.println("服务端收到消息:" +  wrap.slice(0, wrap.position()));
                    }
                    printStream = new PrintStream(accept.getOutputStream());
                    printStream.println("success");
                    printStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("启动成功");
    }
}
